package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.common.dto.moments.CommentInfoDTO;
import com.SmartCare.common.dto.moments.DynamicInfoDTO;
import com.SmartCare.common.dto.moments.ForwardInfoDTO;
import com.SmartCare.common.dto.moments.UpdateCommentInfoDTO;
import com.SmartCare.common.vo.DynamicInfoVo;
import com.SmartCare.pojo.homeInfo.CareHomeDetails;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.ImageService;
import com.SmartCare.service.homeInfo.DetailsService;
import com.SmartCare.service.moments.CommentInfoService;
import com.SmartCare.service.moments.DynamicInfoService;
import com.SmartCare.service.moments.ForwardInfoService;
import com.SmartCare.service.moments.LikeInfoService;
import com.SmartCare.service.role.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/dynamic")
@Api(tags = "C端老友圈")
@Slf4j
public class DynamicController {
    @Resource
    private CommentInfoService commentInfoService;
    @Resource
    private DynamicInfoService dynamicInfoService;
    @Resource
    private ForwardInfoService forwardInfoService;
    @Resource
    private LikeInfoService likeInfoService;
    @Resource
    ImageService imageService;
    @Resource
    private UserService userService;

    @Value("${files.upload.path}/momentsImg/")
    private String fileUploadPath;
    @Resource
    private DetailsService detailsService;

    // 查询所有动态
    @GetMapping("/all")
    public Result getAllDynamic(String careHomeName, Long userId) {
        CareHomeDetails one = detailsService.getOne(new LambdaQueryWrapper<CareHomeDetails>().eq(CareHomeDetails::getName, careHomeName));
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId, userId));
        return dynamicInfoService.getAllDynamic(one.getId(), user.getUserId());

    }

    // 查看自己发表动态
    @GetMapping("/my")
    public Result getMyDynamic(@RequestParam("userId") Long userId) {
        List<DynamicInfoVo> vos = dynamicInfoService.getMyDynamic(userId);
        return Result.success(vos);
    }

    //添加配图
    @PostMapping("/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "dynamic/download");
    }

    //下载活动图片
    @GetMapping("/download/{fileUuid}")
    @Operation(summary = "上传图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    // 发表动态
    @PostMapping("/publish")
    public Result publishDynamic(@RequestBody DynamicInfoDTO dynamic) {
        return dynamicInfoService.publishDynamic(dynamic);
    }

    // 修改动态
    @PutMapping("/update")
    public Result updateDynamic(@RequestParam Long id, @RequestBody DynamicInfoDTO dynamic) {
        return dynamicInfoService.updateDynamic(id, dynamic);
    }

    // 删除动态
    @DeleteMapping("/delete")
    public Result deleteDynamic(@RequestParam("id") Long id) {
        return dynamicInfoService.deleteDynamic(id);
    }

    // 评论动态
    @PostMapping("/comment")
    public Result commentDynamic(@RequestBody CommentInfoDTO comment) {
        return commentInfoService.commentDynamic(comment);
    }

    //修改评论
    @PutMapping("/updateComment")
    public Result updateComment(@RequestBody UpdateCommentInfoDTO comment) {
        return commentInfoService.updateComment(comment);
    }

    //删除评论
    @DeleteMapping("/deleteComment")
    public Result deleteComment(Long id) {
        return commentInfoService.deleteComment(id);
    }

    // 点赞动态
    @GetMapping("/like")
    public Result likeDynamic(Long userId, Long dynamicId) {
        return likeInfoService.likeDynamic(userId, dynamicId);
    }

    //取消点赞
    @DeleteMapping("/cancelLike")
    public Result cancelLikeDynamic(Long id) {
        return likeInfoService.cancelLikeDynamic(id);
    }

    // 转发动态
    @PostMapping("/forward/new")
    public Result forwardDynamic(@RequestBody ForwardInfoDTO forward) {
        return forwardInfoService.forwardDynamic(forward);
    }


    // 取消转发
    @DeleteMapping("/forward/delete")
    public Result deleteForward(Long id) {
        return forwardInfoService.deleteForward(id);
    }

    // 获取养老院的老友圈分页数据
    @GetMapping("/query")
    public Result getPage(@RequestParam Integer page,
                          @RequestParam Integer pageSize,
                          @RequestParam String careHomeName,
                          @RequestParam Long userId) {
        return dynamicInfoService.getPage(page, pageSize, careHomeName, userId);
    }

}
